package com.ruoyi.system.domain.smartai;


import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.List;

@Component
public class ScenarioDataUtils {

    private static RedisCache redisCache;

    @Autowired
    public ScenarioDataUtils(RedisCache redisCache) {
        ScenarioDataUtils.redisCache = redisCache;  // 通过构造函数给静态字段赋值
    }

    // Redis key前缀
    private static final String SCENARIO_KEY_PREFIX = "persona:scenario:";
    public static final String SCENARIO_FANS_KEY_PREFIX = "persona:scenario:fans:";

    /**
     * 保存场景配置
     */
    public static String saveScenario(ScenarioData scenario) {
        String key = SCENARIO_KEY_PREFIX + scenario.getServiceNo();
        redisCache.setCacheObject(key, scenario);
        return "1";
    }

    /**
     * 获取场景配置
     */
    public static ScenarioData getScenario(String customerNumber) {
        String key = SCENARIO_KEY_PREFIX + customerNumber;
        ScenarioData scenarioData = redisCache.getCacheObject(key);
        return scenarioData;
    }

    /**
     * 更新某一天的场景配置
     */
    public void updateDayScenario(String serviceNo, DayScenario dayScenario) {
        ScenarioData scenario = getScenario(serviceNo);
        if (scenario != null) {
            List<DayScenario> days = scenario.getDayScenarios();
            // 更新对应天数的配置
            for (int i = 0; i < days.size(); i++) {
                if (days.get(i).getDayNo().equals(dayScenario.getDayNo())) {
                    days.set(i, dayScenario);
                    break;
                }
            }
            // 保存更新后的配置
            saveScenario(scenario);
        }
    }

    /**
     * 删除场景配置
     */
    public void deleteScenario(String serviceNo) {
        String key = SCENARIO_KEY_PREFIX + serviceNo;
        redisCache.deleteObject(key);
    }


    /**
     * 开始时间
     */
    public static SceneDetail startScenario(String fansId, String customerId) {
        SceneDetail sceneDetail = null;
        try {
            String key = SCENARIO_KEY_PREFIX + customerId;
            ScenarioData scenario = redisCache.getCacheObject(key);
            String currentKey = SCENARIO_FANS_KEY_PREFIX + fansId;
            CurrentScenarioData currentScenarioData = redisCache.getCacheObject(currentKey);
//            if (currentScenarioData == null) {
            currentScenarioData = new CurrentScenarioData();
            currentScenarioData.setCurrentDayNo(1);
            currentScenarioData.setFansId(fansId);
            currentScenarioData.setCustomerId(customerId);
            currentScenarioData.setCurrentRounds(1);
            currentScenarioData.setCurrentScenes(1);
            currentScenarioData.setCurrentTime(DateUtils.getNowDate());
            currentScenarioData.setStartDateTime(DateUtils.getNowDate());
            currentScenarioData.setScenarioTime(DateUtils.getNowDate());
//            }
            List<DayScenario> dayScenarios = scenario.getDayScenarios();
            DayScenario dayScenario = dayScenarios.get(0);
            List<SceneDetail> scenes = dayScenario.getScenes();
            sceneDetail = scenes.get(0);
            if (StringUtils.isNotEmpty(sceneDetail.getPrompt())) {
                currentScenarioData.setPrompt(sceneDetail.getPrompt());
            } else {
                currentScenarioData.setPrompt(dayScenario.getSelfIntro());
            }
            currentScenarioData.setNextTime(sceneDetail.getNextSceneTime());
            currentScenarioData.setContextLength(sceneDetail.getContextLength());

            currentScenarioData.setScenarioName(sceneDetail.getName());
            redisCache.setCacheObject(currentKey, currentScenarioData);
        } catch (Exception e) {
            sceneDetail = null;
        }
        return sceneDetail;
    }

    /**
     * 转换天数
     */
    public static SceneDetail turnDate(String fansId, CurrentScenarioData currentScenarioData, ScenarioData scenario) {
        SceneDetail sceneDetail = null;
        try {
            Integer currentDay = currentScenarioData.getCurrentDayNo();
            String currentKey = SCENARIO_FANS_KEY_PREFIX + fansId;
            if (currentDay < scenario.getDayScenarios().size()) {
                currentScenarioData.setCurrentDayNo(currentDay + 1);
                currentScenarioData.setCurrentRounds(1);
                currentScenarioData.setStartDateTime(DateUtils.getNowDate());
                currentScenarioData.setCurrentScenes(1);
                currentScenarioData.setScenarioTime(DateUtils.getNowDate());
                currentScenarioData.setCurrentTime(DateUtils.getNowDate());
                DayScenario dayScenario = scenario.getDayScenarios().get(currentDay);
                List<SceneDetail> scenes = dayScenario.getScenes();
                sceneDetail = scenes.get(0);
                currentScenarioData.setContextLength(sceneDetail.getContextLength());
                currentScenarioData.setNextTime(sceneDetail.getNextSceneTime());
                if (StringUtils.isNotNull(sceneDetail.getPrompt())) {
                    currentScenarioData.setPrompt(sceneDetail.getPrompt());
                } else {
                    currentScenarioData.setPrompt(dayScenario.getSelfIntro());
                }
                sceneDetail.setOut(false);
                currentScenarioData.setScenarioName(sceneDetail.getName());
                redisCache.setCacheObject(currentKey, currentScenarioData);
                return sceneDetail;
            } else {
                sceneDetail = new SceneDetail();
                sceneDetail.setOut(true);
            }
        } catch (Exception e) {
            sceneDetail = null;
        }
        return sceneDetail;

    }

    /**
     * 转换场景
     */
    public static SceneDetail turnScene(String fansId, CurrentScenarioData currentScenarioData, ScenarioData scenario) {
        SceneDetail sceneDetail = null;
        try {
            String currentKey = SCENARIO_FANS_KEY_PREFIX + fansId;
            Integer currentScene = currentScenarioData.getCurrentScenes();
            Integer currentRounds = currentScenarioData.getCurrentRounds();
            DayScenario dayScenario = scenario.getDayScenarios().get(currentScenarioData.getCurrentDayNo() - 1);
            sceneDetail = dayScenario.getScenes().get(currentScene - 1);
            sceneDetail.setOut(true);
            if (currentRounds <= sceneDetail.getRounds()) {
                currentScenarioData.setCurrentRounds(currentRounds + 1);
                sceneDetail.setOut(false);
                currentScenarioData.setScenarioName(sceneDetail.getName());
                currentScenarioData.setCurrentTime(DateUtils.getNowDate());
                sceneDetail = null;
            }
//            else if (currentScene < dayScenario.getScenes().size()) {
//                sceneDetail = dayScenario.getScenes().get(currentScene);
//                currentScenarioData.setCurrentRounds(1);
//                currentScenarioData.setContextLength(sceneDetail.getContextLength());
//                currentScenarioData.setCurrentScenes(currentScene + 1);
//                currentScenarioData.setNextTime(sceneDetail.getNextSceneTime());
//                currentScenarioData.setScenarioTime(DateUtils.getNowDate());
//                currentScenarioData.setCurrentTime(DateUtils.getNowDate());
//                sceneDetail.setOut(false);
//                if (StringUtils.isNotEmpty(sceneDetail.getPrompt())) {
//                    currentScenarioData.setPrompt(sceneDetail.getPrompt());
//                } else {
//                    currentScenarioData.setPrompt(dayScenario.getSelfIntro());
//                }
//                currentScenarioData.setScenarioName(sceneDetail.getName());
//            } else {
//                sceneDetail = new SceneDetail();
//                sceneDetail.setOut(true);
//            }
            redisCache.setCacheObject(currentKey, currentScenarioData);
            return sceneDetail;
        } catch (Exception e) {
            sceneDetail = null;
        }
        return sceneDetail;
    }

    public static SceneDetail startNextScenario(CurrentScenarioData currentScenarioData) {
        SceneDetail sceneDetail = null;
        try {
            String currentKey = SCENARIO_FANS_KEY_PREFIX + currentScenarioData.getFansId();
            String key = SCENARIO_KEY_PREFIX + currentScenarioData.getCustomerId();
            Integer currentScene = currentScenarioData.getCurrentScenes();
            ScenarioData scenario = redisCache.getCacheObject(key);
            DayScenario dayScenario = scenario.getDayScenarios().get(currentScenarioData.getCurrentDayNo() - 1);

            sceneDetail = dayScenario.getScenes().get(currentScene);
            currentScenarioData.setCurrentRounds(1);
            currentScenarioData.setContextLength(sceneDetail.getContextLength());
            currentScenarioData.setCurrentScenes(currentScene + 1);
            currentScenarioData.setNextTime(sceneDetail.getNextSceneTime());
            currentScenarioData.setScenarioTime(DateUtils.getNowDate());
            currentScenarioData.setCurrentTime(DateUtils.getNowDate());
            sceneDetail.setOut(false);
            if (StringUtils.isNotEmpty(sceneDetail.getPrompt())) {
                currentScenarioData.setPrompt(sceneDetail.getPrompt());
            } else {
                currentScenarioData.setPrompt(dayScenario.getSelfIntro());
            }
            currentScenarioData.setScenarioName(sceneDetail.getName());
            redisCache.setCacheObject(currentKey, currentScenarioData);
            return sceneDetail;
        } catch (Exception e) {
            sceneDetail = null;
        }
        return sceneDetail;
    }

}
